import { FC } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'

import { useAppDispatch } from '@/store'

import { loginAsync } from '@/store/user.store'

const LoginPage: FC = () => {
  const dispatch = useAppDispatch()

  const navigate = useNavigate()
  const location = useLocation()
  const searchParams = new URLSearchParams(location.search)
  const from: string = searchParams.get('from')!

  const toLogin = async () => {
    const res = await dispatch(loginAsync())
    console.log('res', res)

    res && navigate(`${from}`, { replace: true })
  }

  return <div onClick={() => toLogin()}>
    LoginPage
  </div>
}

export default LoginPage
